use strict;
use warnings;
use Test::More;
use Circle::Miner;
use Config;
use Carp;

# Test module loading
BEGIN { use_ok('Circle::Miner') }

# Test get_os_arch function
subtest 'get_os_arch tests' => sub {
    my ($os, $arch) = Circle::Miner::get_os_arch();
    carp "os: $os, arch: $arch";

    # Test OS detection
    if ($^O =~ /linux/i) {
        is($os, 'linux', 'Should detect Linux OS');
    } elsif ($^O =~ /darwin/i) {
        is($os, 'mac', 'Should detect Mac OS');
    } elsif ($^O =~ /win/i) {
        is($os, 'windows', 'Should detect Windows OS');
    }
    
    # Test architecture detection
    my $system_arch = $Config{archname};
    carp "system_arch: $system_arch";
    $system_arch =~ s/-.*$//;
    carp "system_arch: $system_arch";
    if ($system_arch =~ /^x86_64|amd64/i) {
        is($arch, 'x86_64', $system_arch . 'Should detect x86_64 architecture');
    } elsif ($system_arch =~ /^i[3456]86/i) {
        is($arch, 'x86', $system_arch . 'Should detect x86 architecture');
    } elsif ($system_arch =~ /^arm64|aarch64/i) {
        is($arch, 'arm64', $system_arch . 'Should detect ARM64 architecture');
    }
    
    # Test return format
    ok(defined $os, 'OS should be defined');
    ok(defined $arch, 'Architecture should be defined');
    like($os, qr/^(linux|mac|windows)$/, 'OS should be one of linux, mac, or windows');
    like($arch, qr/^(x86_64|x86|arm64|arm)$/, 'Architecture should be one of x86_64, x86, or arm64, or arm');
};

# Test architecture normalization
subtest 'architecture normalization tests' => sub {    
    my $arch = Circle::Miner::get_arch_by_config('x86_64-linux');
    is($arch, 'x86_64', 'Should normalize x86_64');
    
    $arch = Circle::Miner::get_arch_by_config('amd64-linux-thread-multi');
    is($arch, 'x86_64', 'Should normalize amd64 to x86_64');
    
    $arch = Circle::Miner::get_arch_by_config('i686-linux');
    is($arch, 'x86', 'Should normalize i686 to x86');
    
    $arch = Circle::Miner::get_arch_by_config('aarch64-linux');
    is($arch, 'arm64', 'Should normalize aarch64 to arm64');
};

done_testing(); 
